package org.jeecg.modules.demo.test.service.impl;

import org.jeecg.modules.demo.test.entity.School;
import org.jeecg.modules.demo.test.entity.Student;
import org.jeecg.modules.demo.test.mapper.StudentMapper;
import org.jeecg.modules.demo.test.mapper.SchoolMapper;
import org.jeecg.modules.demo.test.service.ISchoolService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 学校
 * @Author: jeecg-boot
 * @Date:   2022-01-24
 * @Version: V1.0
 */
@Service
public class SchoolServiceImpl extends ServiceImpl<SchoolMapper, School> implements ISchoolService {

	@Autowired
	private SchoolMapper schoolMapper;
	@Autowired
	private StudentMapper studentMapper;
	
	@Override
	@Transactional
	public void saveMain(School school, List<Student> studentList) {
		schoolMapper.insert(school);
		if(studentList!=null && studentList.size()>0) {
			for(Student entity:studentList) {
				//外键设置
				entity.setSchoolId(school.getId());
				studentMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(School school,List<Student> studentList) {
		schoolMapper.updateById(school);
		
		//1.先删除子表数据
		studentMapper.deleteByMainId(school.getId());
		
		//2.子表数据重新插入
		if(studentList!=null && studentList.size()>0) {
			for(Student entity:studentList) {
				//外键设置
				entity.setSchoolId(school.getId());
				studentMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		studentMapper.deleteByMainId(id);
		schoolMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			studentMapper.deleteByMainId(id.toString());
			schoolMapper.deleteById(id);
		}
	}
	
}
